#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=1e6+5;

int a[N],b[N],d[N],c[N],n,p;
ll res;

int solve(int x,int d2){
	if (b[x] == 2){
		return c[x];
	}
	if (b[x] == 1){
		p = x;
		return d2 - d[x];
	}
	b[x] = 1,d[x] = d2;
	c[x] = solve(a[x],d2 + 1) + !p;
	x == p && (p = 0);
	b[x] = 2;
	return c[x]; 
}

int main(){
	cin >> n;
	for (int i = 1;i <= n;i++){
		cin >> a[i];
	}
	for (int i = 1;i <= n;i++){
		res += solve(i,0);
	}
	cout << res << endl;;
	return 0;
}